﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using CMN.Controllers;
using CMN.Controllers.Filters;
using CMN.Common;
using System.Text;
using CMN.UI;
using CMN.Framework;
using CMN.BLL;
using CMN.Config;
using System.Diagnostics;
namespace CMN.Controllers
{
    
    [LoggerFilter()]
    [ExceptionFilter()]
    [ExecutionTimingAttribute()]
    public class ArticleController : Controller
    {
        private Stopwatch timer;
        [CompressFilter]
        [CacheFilterAttribute]
        public string Index(string channel, string channel2, string id, string type)
        {
            //this.timer = new Stopwatch();
            //this.timer.Start();
            
            PageHelperEntity pageHelper = new PageHelperEntity();
            Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channel2);
            if (!string.IsNullOrEmpty(type))
            {
                if (type == "wap")
                    pageHelper.TType = TemplateType.Phone;
            }
            if (channelEntity == null)
            {
                Utils.Redirect404();
                return "";
            }

            pageHelper.Channel = channelEntity;
            pageHelper.ChannelId = channelEntity.Id;
            pageHelper.FromUrl = Utils.GetUrlReferrer();
            pageHelper.SearchWords = "";
           
            

            
            try
            {
                if (string.IsNullOrEmpty(channel))
                {


                    if (pageHelper.Channel.ReId == 0)
                    {
                        pageHelper.ParentChannel = pageHelper.Channel;
                        pageHelper.ParentDir = pageHelper.Channel.Dir;
                        pageHelper.ParentChannelId = pageHelper.Channel.Id;
                    }
                    else
                    {
                        Normal_ChannelEntity parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(pageHelper.Channel.ReId);
                        if (parentChannelEntity.ReId == 0)
                        {
                            pageHelper.ParentDir = parentChannelEntity.Dir;
                            pageHelper.ParentChannelId = parentChannelEntity.Id;
                            pageHelper.ParentChannel = parentChannelEntity;
                        }
                        else
                        {
                            parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(parentChannelEntity.ReId);
                            pageHelper.ParentDir = parentChannelEntity.Dir;
                            pageHelper.ParentChannelId = parentChannelEntity.Id;
                            pageHelper.ParentChannel = parentChannelEntity;
                        }
                    }
                }
                else
                {
                    Normal_ChannelEntity parentChannelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channel);

                    //以前有些地址改变了
                    if (parentChannelEntity != null)
                    {
                        pageHelper.ParentDir = parentChannelEntity.Dir;
                        pageHelper.ParentChannelId = parentChannelEntity.Id;
                        pageHelper.ParentChannel = parentChannelEntity;
                    }
                    else
                    {
                        if (pageHelper.Channel.ReId == 0)
                        {
                            pageHelper.ParentDir = pageHelper.Channel.Dir;
                            pageHelper.ParentChannelId = pageHelper.Channel.Id;
                            pageHelper.ParentChannel = parentChannelEntity;
                        }
                        else
                        {
                            parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(pageHelper.Channel.ReId);
                            if (parentChannelEntity.ReId == 0)
                            {
                                pageHelper.ParentDir = parentChannelEntity.Dir;
                                pageHelper.ParentChannelId = parentChannelEntity.Id;
                                pageHelper.ParentChannel = parentChannelEntity;
                            }
                            else
                            {
                                parentChannelEntity = MvcWebCacheManager.GetNormal_Channel(parentChannelEntity.ReId);
                                pageHelper.ParentDir = parentChannelEntity.Dir;
                                pageHelper.ParentChannelId = parentChannelEntity.Id;
                                pageHelper.ParentChannel = parentChannelEntity;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AppLog.Write(ex.Message, AppLog.LogMessageType.Error, ex);
            }
            string[] strparam = id.Split('_');  //是否分页
            string contentId = string.Empty;
            switch (strparam.Length)
            {
                case 1:
                    {
                        contentId = id;
                        break;
                    }

                case 2:
                    {
                        contentId = strparam[0];
                        pageHelper.PageIndex = Utils.StringToInt(strparam[1], 1);
                        break;
                    }
            }

            pageHelper.ContentId = Utils.StringToInt(contentId, 0);
            pageHelper.Url = string.Concat(pageHelper.Channel.Url, "/", pageHelper.ContentId.ToString());
            if (pageHelper.ContentId == 0)
            {
                Utils.Redirect404();
                return "";
            }
            pageHelper.Type = PageType.ContentPage;

            //this.timer.Stop();
            //string strTemp = timer.ElapsedMilliseconds.ToString();
            //this.timer.Start();
            BaseTemplateDisplay articleTemplate = new BaseTemplateDisplay(pageHelper);

            //this.timer.Stop();
            //strTemp = string.Concat(strTemp, "-----2----", timer.ElapsedMilliseconds.ToString());
            //this.timer.Start();
            string strRetrun = articleTemplate.Display();
            //articleTemplate.Dispose();
            //this.timer.Stop();
            //strTemp = string.Concat(strTemp, "-----3----", timer.ElapsedMilliseconds.ToString());
            //return strTemp + strRetrun;
            return strRetrun;

        }

        public string Update(int id, string channel, string cmd)
        {
            int num = 0;
            string strTemp = string.Empty;
            string strReturn = string.Empty;
             Normal_ChannelEntity channelEntity = MvcWebCacheManager.GetNormal_ChannelByDir(channel);
             if (channelEntity == null)  
             {
                 return strReturn = "{\"result\":\"0\",\"returnval\":\"错误的栏目信息\"}";
             }
             BLLHelper bll = new BLLHelper();
            switch (cmd)
            {
                case "upclick":
                    {
                        try
                        {
                            Normal_ClickRecordEntity clickRecord = new Normal_ClickRecordEntity();
                            clickRecord.AddDate = DateTime.Now;
                            clickRecord.ModuleType = channelEntity.Path;
                            clickRecord.Ip = Utils.GetIP();
                            clickRecord.Id = id;
                            clickRecord.DataTable_Action = DataTableAction.Insert;
                            MvcWebCacheManager.SaveNormal_ClickRecordList(clickRecord);
                            strReturn = "{\"result\":\"1\",\"returnval\":\"\"}";


                        }
                        catch (Exception ex)
                        {
                            strReturn = "{\"result\":\"-1\",\"returnval\":\"" + ex.Message + "\"}";
                        }
                        break;
                    }
                case "upsupport":
                    {
                        try
                        {
                            IDataService<Normal_SupportRecordEntity> Normal_SupportRecordSv = ServiceBuilder.BuildNormal_SupportRecordService();
                            QueryParam qp = new QueryParam();
                            qp.Where = string.Concat("ModuleType='", channelEntity.Path, "' and ip='", Utils.GetIP(), "' and  id=", id.ToString());
                            int recordCount=0;
                            qp.PageSize = 1;
                            qp.PageIndex = 1;
                            Normal_SupportRecordSv.List(qp, out recordCount);
                            if (recordCount>0)
                            {
                                strReturn = "{\"result\":\"-1\",\"returnval\":\"您已经推荐过了！\"}";
                            }
                            else
                            {
                                Normal_SupportRecordEntity support = new Normal_SupportRecordEntity();
                                support.Id = id;
                                support.AddDate = DateTime.Now;
                                support.Ip = Utils.GetIP();
                                support.ModuleType = channelEntity.Path;
                                support.DataTable_Action = DataTableAction.Insert;
                                Normal_SupportRecordSv.Insert(support);

                                num = bll.Update(string.Concat("Module_", channelEntity.ChannelType), "supportnum=supportnum+1", string.Concat("id=", id.ToString()));

                                if (num > 0)
                                {
                                   
                                    strReturn = "{\"result\":\"1\",\"returnval\":\"\"}";
                                }
                                else
                                {
                                    strReturn = "{\"result\":\"0\",\"returnval\":\" \"}";
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            strReturn = "{\"result\":\"-1\",\"returnval\":\"" + ex.Message + "\"}";
                        }
                        break;
                    }
                case "upagainst":
                    {
                        try
                        {
                            IDataService<Normal_AgainstRecordEntity> Normal_AgainstRecordSv = ServiceBuilder.BuildNormal_AgainstRecordService();
                            QueryParam qp = new QueryParam();
                            qp.Where = string.Concat("ModuleType='", channelEntity.Path, "' and ip='", Utils.GetIP(), "' and  id=", id.ToString());
                            int recordCount = 0;
                            qp.PageSize = 1;
                            qp.PageIndex = 1;
                            Normal_AgainstRecordSv.List(qp, out recordCount);
                            if (recordCount>0)
                            {
                                strReturn = "{\"result\":\"-1\",\"returnval\":\"您已经反对过了！\"}";
                            }
                            else
                            {
                                Normal_AgainstRecordEntity against = new Normal_AgainstRecordEntity();
                                against.ModuleType = channelEntity.Path;
                                against.AddDate = DateTime.Now;
                                against.Id = id;
                                against.Ip = Utils.GetIP();
                                against.DataTable_Action = DataTableAction.Insert;
                                Normal_AgainstRecordSv.Insert(against);
                                num = bll.Update(string.Concat("Module_", channelEntity.ChannelType), "againstnum=againstnum+1", string.Concat("id=", id.ToString()));
                                if (num > 0)
                                {
                                    
                                    strReturn = "{\"result\":\"1\",\"returnval\":\"\"}";
                                }
                                else
                                {
                                    strReturn = "{\"result\":\"0\",\"returnval\":\"\"}";
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            strReturn = "{\"result\":\"-1\",\"returnval\":\"" + ex.Message + "\"}";
                        }
                        break;
                    }
                default:
                    {
                        strReturn = "{\"result\":\"-1\",\"returnval\":\"无效参数\"}";
                        break;
                    }
                   


            }
            return strReturn;
        }
        
    }
}
